package com.vcredit.creditcat.manager;

import java.util.List;
import java.util.Map;

import com.vcredit.creditcat.service.speedloan.ProcessSpecialService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.vcredit.creditcat.api.IAppVersionManager;
import com.vcredit.creditcat.model.AppVersion;
import com.vcredit.creditcat.service.AppVersionService;
import com.vcredit.framework.util.JacksonUtil;

@Service(protocol = { "protocol1" }, version = "1.0")
public class AppVersionManager implements IAppVersionManager {
    @Autowired
	private AppVersionService appVersionService;
	@Autowired
	private ProcessSpecialService processSpecialService;
	@Override
	public AppVersion getAppVersion() {
		return appVersionService.findAll().get(0);
	}
	@Override
	public AppVersion getAppVersion(String appDesc) throws Exception {
		if(StringUtils.isBlank(appDesc)){
			return getDefaultAppVersion(null);
		}
		List<AppVersion> appVersionList=appVersionService.findByAppDesc(appDesc);
		if(appVersionList.size()==1){
			return appVersionList.get(0);
		}
		String[] versionInfo = appDesc.split(";");
		return getDefaultAppVersion(versionInfo[0]);
	}

	@Override
	public Long findNewFastProduct(String appDesc) {
		return processSpecialService.findNewFastProduct(appDesc);
	}

	private AppVersion getDefaultAppVersion(String appName) throws Exception{
		AppVersion obj = new AppVersion();
		if(StringUtils.isBlank(appName)){
			obj.setAppName("creditcat");
		}else{
			obj.setAppName(appName);
		}
		return appVersionService.find(obj).get(0);
	}
}
